package com.my.algorithmlearning.leetcode;

import com.my.algorithmlearning.entity.TreeNode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

/**
 * @ClassName MergeTrees
 * @Description MergeTrees
 * @Author wanghaiwei
 * @Date 2023/1/29 18:23
 */
public class MergeTrees {

    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        TreeNode result;
        if (root1 == null && root2 == null) {
            return null;
        } else if (root1 == null && root2 != null) {
            result = root2;
            return result;
        } else if (root1 != null && root2 == null) {
            result = root1;
            return result;
        } else {
            result = new TreeNode(root1.val + root2.val);
        }

        result.left = mergeTrees(root1.left, root2.left);
        result.right = mergeTrees(root1.right, root2.right);
        return result;
    }

    public static void main(String[] args) {
        TreeNode root1 = new TreeNode(new Integer[]{1, 3, 2, 5});
        TreeNode root2 = new TreeNode(new Integer[]{2, 1, 3, null, 4, null, 7});
        MergeTrees solution = new MergeTrees();
        String result = solution.mergeTrees(root1, root2).toString();
        Assert.isTrue("3,4,5,5,4,7".equals(result), "错误，实际输出：" + result);
    }
}
